package com.elm.controller;

import java.io.IOException;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import com.elm.model.Food;
import com.elm.model.ShoppingCar;
import com.elm.model.User;
import com.elm.service.FoodService;
import com.elm.service.ShoppingCarService;
import com.elm.serviceimpl.FoodServiceImpl;
import com.elm.serviceimpl.ShoppingCarServiceImpl;
/**

 * 

 * @author pgm

 * 

 */
public class ShoppingCarController extends HttpServlet{
	ShoppingCarService cservice = new ShoppingCarServiceImpl();
	FoodService fservice = new FoodServiceImpl();
	@Override
	protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		// TODO Auto-generated method stub


		doPost(req, resp);
	}
	@Override
	protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		req.setCharacterEncoding("utf8");
		HttpSession session = req.getSession();
		User user = (User) session.getAttribute("currentUser");
		//得到购物车内的所以信息
		List<ShoppingCar> allCarInfo = cservice.getAllFoodIdByUserid(user.getUserid());
		//得到foodid 对应的food信息


		List<Food> foods = new ArrayList<Food>();
		for (ShoppingCar shoppingCar : allCarInfo) {
			Food food = new Food();
			food = fservice.getFoodbyFoodid(shoppingCar.getFoodid());
			foods.add(food);
		}
		session.setAttribute("carfoods", foods);
		session.setAttribute("carinfo", allCarInfo);
		//req.setAttribute("carfoods", foods);
		req.getRequestDispatcher("user/shoppingcar.jsp").forward(req, resp);
	}
}
